<template>
  <div>
    <navBar :navTitle="$t('Jijinmima')" />

    <div class="full-width92">
      <van-form ref="refFormPassword" class="public-form">
        <div v-if="ifTpwd">
          <div class="inp-label">{{ $t("Dangqianmima") }}</div>
          <van-field
            v-model.trim="formPassword.old_password"
            class="bg-inp"
            :placeholder="$t('Qingshurumima')"
            clearable
            type="password"
            :rules="[{ required: true, message: $t('Qingshurumima') }]"
          />
        </div>
        <div>
          <div class="inp-label">{{$t('Xinmima')}}</div>
          <van-field
            v-model.trim="formPassword.t_password"
            class="bg-inp"
            :placeholder="$t('Xinmima')"
            clearable
            type="password"
            :rules="[{ required: true, message: $t('Qingshuru') + $t('Xinmima') }]"
          />
        </div>

        <div>
          <div class="inp-label">{{ $t("Querenxinmima") }}</div>
          <van-field
            v-model.trim="formPassword.check_password"
            class="bg-inp"
            :placeholder="$t('Querenxinmima')"
            clearable
            type="password"
            :rules="[
              { required: true, message: $t('Querenxinmima') },
              { validator: checkPass, message: $t('Qingshuru') + $t('Querenxinmima') },
            ]"
          />
        </div>
      </van-form>

      <van-button
        block
        type="info"
        native-type="submit"
        class="marTop40"
        @click="fnBtnSave"
        v-btn-re-click
        >{{ $t("Chongzhi") }}</van-button
      >
    </div>
  </div>
</template>

<script>
import { quser_edit_api } from "@/assets/js/api";
import { quser_if_tpwd_api } from "../../assets/js/api";
export default {
  name: "myPassword",
  data() {
    return {
      ifTpwd: true /* 是否显示 旧資金密碼 輸入框 */,
      formPassword: {
        old_password: "",
        t_password: "",
        check_password: "",
      },
    };
  },
  methods: {
    /*確認密碼 验证*/
    checkPass(newPass) {
      console.log(newPass);
      return newPass === this.formPassword.t_password;
      // return /^1\d{10}$/.test(val) // 返回true或false
    },

    fnBtnSave() {
      this.$refs.refFormPassword
        .validate()
        .then(() => {
          quser_edit_api({
            id: localStorage.getItem("userId"),

            t_password: this.formPassword.t_password,
            old_password: this.formPassword.old_password,
          }).then((res) => {
            if (res.code == 200) {
              this.$toast(res.msg || "Đặt lại mật khẩu thành công！");

              setTimeout(() => {
                this.$router.push({
                  name: "my",
                });
              }, 1000);
            } else {
              this.$toast(this.$t('Dngaqianmimacuo'));
            }
          });
        })
        .catch((res) => {});
    },

    /*验证是否有二级密碼*/
    fnCheckPass() {
      quser_if_tpwd_api().then((res) => {
        if (res.code == 201) {
          this.ifTpwd = false;
        }
      });
    },
  },
  created() {
    this.fnCheckPass();
  },
};
</script>
